/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Courseteacherassign;
import me.zhengjie.modules.system.domain.CourseteacherassignVo;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.domain.vo.CourseteacherassignQueryCriteria;
import me.zhengjie.modules.system.service.CourseteacherassignService;

import me.zhengjie.modules.system.service.TeacherService;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.TimeUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author shitou
* @date 2025-06-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程教师分配管理")
@RequestMapping("/api/courseteacherassign")
public class CourseteacherassignController {

    private final CourseteacherassignService courseteacherassignService;

    private final TeacherService teacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('courseteacherassign:list')")
    public void exportCourseteacherassign(HttpServletResponse response, CourseteacherassignQueryCriteria criteria) throws IOException {
        courseteacherassignService.download(courseteacherassignService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程教师分配")
    @ApiOperation("查询课程教师分配")
//    @PreAuthorize("@el.check('courseteacherassign:list')")
    public ResponseEntity<List<Teacher>> queryCourseteacherassign(CourseteacherassignQueryCriteria criteria, Page<Object> page){
//        return new ResponseEntity<>(courseteacherassignService.queryAll(criteria,page),HttpStatus.OK);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("courseId",criteria.getCourseId());
        List<Courseteacherassign> courseteacherassignList = courseteacherassignService.list(queryWrapper);
        List<Integer> userIds = new ArrayList<>();
        for(Courseteacherassign courseteacherassign:courseteacherassignList){
            userIds.add(courseteacherassign.getUserId());
        }

        List<Teacher> teacherList = new ArrayList<>();
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("userId",userIds);
        if(userIds.size()!=0){

            teacherList = teacherService.list(queryWrapper1);
        }
        return new ResponseEntity<>(teacherList,HttpStatus.OK);
    }

    @GetMapping("/getTeacherCourseList")
    @Log("查询课程教师分配")
    @ApiOperation("查询课程教师分配")
    public ResponseEntity<Object> createCourseteacherassign(@Validated @RequestBody CourseteacherassignQueryCriteria resources){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",resources.getUserId());
        List<Courseteacherassign> courseteacherassignList = courseteacherassignService.list(queryWrapper);


        return new ResponseEntity<>(courseteacherassignList,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增课程教师分配")
    @ApiOperation("新增课程教师分配")
//    @PreAuthorize("@el.check('courseteacherassign:add')")
    public ResponseEntity<Object> createCourseteacherassign(@Validated @RequestBody CourseteacherassignVo resources){
//        courseteacherassignService.create(resources);
//        return new ResponseEntity<>(HttpStatus.CREATED);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("courseId",resources.getCourseId());
        courseteacherassignService.remove(queryWrapper);

        //绑定教师
        for(Integer i =0; i<resources.getUserIds().size();i++){
            Courseteacherassign courseteacherassign = new Courseteacherassign();
            courseteacherassign.setCourseId(resources.getCourseId());
            Integer userId = resources.getUserIds().get(i);
            courseteacherassign.setUserId(userId);
            courseteacherassign.setCreateTime(TimeUtils.getCurrentDateTimeStr());
            courseteacherassignService.create(courseteacherassign);
        }

        return new ResponseEntity<>(HttpStatus.CREATED);
    }
//

    @PostMapping("/createCourseteacher")
    @Log("新增课程教师分配")
    @ApiOperation("新增课程教师分配")
//    @PreAuthorize("@el.check('courseteacherassign:add')")
    public ResponseEntity<Object> createCourseteacherassign2(@Validated @RequestBody CourseteacherassignVo resources){

        //删除该课程所有绑定的教师
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("courseId",resources.getCourseId());
        courseteacherassignService.remove(queryWrapper);

        //绑定教师
        for(Integer i =0; i<resources.getUserIds().size();i++){
            Courseteacherassign courseteacherassign = new Courseteacherassign();
            courseteacherassign.setCourseId(resources.getCourseId());
            Integer userId = resources.getUserIds().get(i);
            courseteacherassign.setUserId(userId);
            courseteacherassign.setCreateTime(TimeUtils.getCurrentDateTimeStr());
            courseteacherassignService.create(courseteacherassign);
        }

        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    @PutMapping
    @Log("修改课程教师分配")
    @ApiOperation("修改课程教师分配")
//    @PreAuthorize("@el.check('courseteacherassign:edit')")
    public ResponseEntity<Object> updateCourseteacherassign(@Validated @RequestBody Courseteacherassign resources){
        courseteacherassignService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除课程教师分配")
    @ApiOperation("删除课程教师分配")
//    @PreAuthorize("@el.check('courseteacherassign:del')")
    public ResponseEntity<Object> deleteCourseteacherassign(@RequestBody List<Integer> ids) {
        courseteacherassignService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}